package net.soevar.action;

import java.util.Date;

import net.soevar.common.Common;
import net.soevar.common.DesEncrypter;
import net.soevar.database.hibernate.HibernateUtil;
import net.soevar.database.model.AthRole;
import net.soevar.database.model.AthUser;
import net.soevar.database.model.Jobseeker;

import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericAutowireComposer;
import org.zkoss.zul.Label;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

public class RegisterAction extends GenericAutowireComposer {

	private static final long serialVersionUID = 1522556050860153845L;
	private Window window;
	private Textbox username;
	private Textbox email;
	private Textbox password;
	private Textbox repeatPassword;

	private Textbox usernameJobseeker;
	private Textbox emailJobseeker;
	private Textbox passwordJobseeker;
	private Textbox repeatPasswordJobseeker;

	private Label commentUsername;
	private Label commentUsernameJobseeker;

	private DesEncrypter desEncrypter = new DesEncrypter(Common.DES_PASS_PHRASE);

	public void onRegisterJob(Event event) throws Exception {
		if (onSaveJob(event)) {
			Messagebox.show("Pendaftaran Berhasil", "Peringatan",
					Messagebox.OK, Messagebox.EXCLAMATION);
			window.detach();
		} else {
			Messagebox.show("Pendaftaran Gagal", "Peringatan", Messagebox.OK,
					Messagebox.EXCLAMATION);

		}
	}
	
	public void onRegisterJobseeker(Event event) throws Exception {
		if (onSaveJobseeker(event)) {
			Messagebox.show("Pendaftaran Berhasil", "Peringatan",
					Messagebox.OK, Messagebox.EXCLAMATION);
			window.detach();
		} else {
			Messagebox.show("Pendaftaran Gagal", "Peringatan", Messagebox.OK,
					Messagebox.EXCLAMATION);

		}
	}

	public boolean onSaveJob(Event event) throws Exception {
		System.out.println(" username: " + username.getValue() + "\n email: "
				+ email.getValue() + "\n password: " + password.getValue()
				+ "\n repeatPassword: " + repeatPassword.getValue());

		// System.out.println("check user: " + checkUserId(email.getValue()));

		if (!password.getValue().trim()
				.equals(repeatPassword.getValue().trim())) {
			Messagebox.show("Password no match", "Peringatan", Messagebox.OK,
					Messagebox.EXCLAMATION);
			return false;
		}

		if (checkUserId(email.getValue())) {
			Messagebox.show("Email unavailable", "Peringatan", Messagebox.OK,
					Messagebox.EXCLAMATION);
			return false;
		}

		Session session = HibernateUtil.currentSession();
		AthUser user = new AthUser();
		user.setUserId(email.getValue());
		user.setUserNama(username.getValue());
		user.setUserPassword(desEncrypter.encrypt(password.getValue()));
		user.setIsEncripted(true);
		user.setDirubahOleh("External User");
		user.setDirubahTanggal(new Date());
		user.setUserRole(new AthRole("employer"));
		user.setStatusAktif(false);
		user.setStatusApproval(false);
		user.setStatusRoot(false);

		session.save(user);

		return true;
	}

	public boolean onSaveJobseeker(Event event) throws Exception {
		System.out.println(" usernameJobseeker: "
				+ usernameJobseeker.getValue() + "\n emailJobseeker: "
				+ emailJobseeker.getValue() + "\n passwordJobseeker: "
				+ passwordJobseeker.getValue() + "\n repeatPasswordJobseeker: "
				+ repeatPasswordJobseeker.getValue());

		// System.out.println("check user: " + checkUserId(email.getValue()));

		if (!passwordJobseeker.getValue().trim()
				.equals(repeatPasswordJobseeker.getValue().trim())) {
			Messagebox.show("Password no match", "Peringatan", Messagebox.OK,
					Messagebox.EXCLAMATION);
			return false;
		}

		if (checkUserIdJobseeker(emailJobseeker.getValue())) {
			Messagebox.show("Email unavailable", "Peringatan", Messagebox.OK,
					Messagebox.EXCLAMATION);
			return false;
		}

		Session session = HibernateUtil.currentSession();
		Jobseeker user = new Jobseeker();
		user.setJobseekerId(emailJobseeker.getValue());
		user.setJobseekerNama(usernameJobseeker.getValue());
		user.setJobseekerPassword(desEncrypter.encrypt(passwordJobseeker
				.getValue()));
		user.setIsEncripted(true);
		user.setDirubahOleh("External User");
		user.setDirubahTanggal(new Date());
		user.setStatusAktif(false);
		user.setStatusApproval(false);

		session.save(user);

		return true;
	}

	public void onCheckEmail(Event event) throws Exception {
		if (!checkUserId(email.getValue())) {
			commentUsername.setStyle("color: #1fab07");
			// commentUsername.setValue("<img src=\"include/img/icon-ok.png\" />Available");
			commentUsername.setValue("Available");

		} else {
			commentUsername.setStyle("color: #e70303");
			commentUsername.setValue("Unavailable");
		}
	}

	public void onCheckEmailJobseeker(Event event) throws Exception {
		if (!checkUserIdJobseeker(emailJobseeker.getValue())) {
			commentUsernameJobseeker.setStyle("color: #1fab07");
			// commentUsername.setValue("<img src=\"include/img/icon-ok.png\" />Available");
			commentUsernameJobseeker.setValue("Available");

		} else {
			commentUsernameJobseeker.setStyle("color: #e70303");
			commentUsernameJobseeker.setValue("Unavailable");
		}
	}

	public boolean checkUserId(String userid) {
		Session session = HibernateUtil.currentSession();
		AthUser user = (AthUser) session.createCriteria(AthUser.class)
				.add(Restrictions.eq("userId", userid)).uniqueResult();
		// session.close();

		if (user != null) {
			return true;
		} else {
			return false;
		}
	}

	public boolean checkUserIdJobseeker(String userid) {
		Session session = HibernateUtil.currentSession();
		Jobseeker jobseeker = (Jobseeker) session
				.createCriteria(Jobseeker.class)
				.add(Restrictions.eq("jobseekerId", userid)).uniqueResult();
		// session.close();

		if (jobseeker != null) {
			return true;
		} else {
			return false;
		}
	}

}
